<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>使用redis实现浏览数、点赞数、评论数的更新 | WEI BLOG</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  
  
    <link rel="alternate" href="/wei-blog/atom.xml" title="WEI BLOG" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/wei-blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/wei-blog/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a id="logo">WEI BLOG</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        
          <a class="main-nav-link" href="/wei-blog/."><i class="fa fa-home"></i> 首页</a>
        
          <a class="main-nav-link" href="/wei-blog/archives/"><i class="fa fa-archive"></i> 归档</a>
        
      </nav>
    </div>
    <div id="search-form">
      <div id="result-mask" class="hide"></div>
      <label><input id="search-key" type="text" autocomplete="off" placeholder="搜索"></label>
      <div id="result-wrap" class="hide">
        <div id="search-result"></div>
      </div>
      <div class="hide">
        <template id="search-tpl">
          <div class="item">
            <a href="/{path}" title="{title}">
              <div class="title">{title}</div>
              <!-- <div class="time">{date}</div> -->
              <div class="tags">{tags}</div>
            </a>
          </div>
        </template>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-使用redis实现浏览数、点赞数、评论数的更新" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      使用redis实现浏览数、点赞数、评论数的更新
    </h1>
  


      </header>
    
    <div class="article-meta">
      
      <!-- <span class="article-date">
  <i class="fa fa-date"></i>
  <time class="dt-published" datetime="2023-02-06T09:03:59.000Z" itemprop="datePublished">2023年02月06日</time>
</span> -->
      
  <div class="article-category">
    <i class="fa fa-classify"></i>
    <a class="article-category-link" href="/wei-blog/categories/Redis/">Redis</a>
  </div>

      
        <span class="article-views">
  <!-- <i class="fa fa-views"></i>
  <i id="busuanzi_container_page_pv">
      <i id="busuanzi_value_page_pv"></i>
  </i> -->
</span>

      
      
<!-- <a href="/wei-blog/source/%E4%BD%BF%E7%94%A8redis%E5%AE%9E%E7%8E%B0%E6%B5%8F%E8%A7%88%E6%95%B0%E3%80%81%E7%82%B9%E8%B5%9E%E6%95%B0%E3%80%81%E8%AF%84%E8%AE%BA%E6%95%B0%E7%9A%84%E6%9B%B4%E6%96%B0/#comments" class="article-comment-link">
  
    
    
    
    
    
  
  <i class="fa fa-commt"></i>
  留言
</a> -->


    </div>
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="执行逻辑"><a href="#执行逻辑" class="headerlink" title="执行逻辑"></a>执行逻辑</h1><p>当获取 post 时，将 post 的各数值信息保存到 Redis 中，定时任务每隔30秒获取一次保存的值，通过 isUpdate 判断该值是否更新过，如更新过则将该值保存到数据库中，如未更新过则根据 updateTimeStamp 判断该值未更新时间间隔，如大于30分钟则删除该键。</p>
<p>RedisKey <code>post:count:&#123;postID&#125;</code></p>
<p>Redis 数据结构</p>
<table>
<thead>
<tr>
<th>field</th>
<th>value</th>
</tr>
</thead>
<tbody><tr>
<td>view</td>
<td>9</td>
</tr>
<tr>
<td>reply</td>
<td>4</td>
</tr>
<tr>
<td>updateTimeStamp</td>
<td>1675674541</td>
</tr>
<tr>
<td>like</td>
<td>3</td>
</tr>
<tr>
<td>isUpdate</td>
<td>1</td>
</tr>
</tbody></table>
<h1 id="逻辑代码"><a href="#逻辑代码" class="headerlink" title="逻辑代码"></a>逻辑代码</h1><h2 id="业务代码"><a href="#业务代码" class="headerlink" title="业务代码"></a>业务代码</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">String</span> <span class="variable">countRedisKey</span> <span class="operator">=</span> RedisKey.POST_COUNT_PREFIX + postVo.getId();</span><br><span class="line"><span class="comment">// 判断 key 是否已经存在</span></span><br><span class="line"><span class="keyword">if</span> (redisUtil.hasKey(countRedisKey)) &#123;</span><br><span class="line">    <span class="comment">// 已存在则增加浏览量</span></span><br><span class="line">    redisUtil.incrementHashValue(countRedisKey, <span class="string">&quot;view&quot;</span>, <span class="number">1L</span>);</span><br><span class="line">    <span class="comment">// 更新isUpdate和timeStamp</span></span><br><span class="line">    Map&lt;String, Object&gt; updateMap = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;(<span class="number">4</span>);</span><br><span class="line">    updateMap.put(<span class="string">&quot;isUpdate&quot;</span>, <span class="number">1</span>);</span><br><span class="line">    updateMap.put(<span class="string">&quot;updateTimeStamp&quot;</span>, LocalDateTime.now().toEpochSecond(ZoneOffset.of(<span class="string">&quot;+8&quot;</span>)));</span><br><span class="line">    redisUtil.setHashBatch(countRedisKey, updateMap);</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="comment">// 不存在则保存</span></span><br><span class="line">    Map&lt;String, Object&gt; countMap = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;(<span class="number">4</span>);</span><br><span class="line">    countMap.put(<span class="string">&quot;isUpdate&quot;</span>, <span class="number">1</span>);</span><br><span class="line">    countMap.put(<span class="string">&quot;updateTimeStamp&quot;</span>, LocalDateTime.now().toEpochSecond(ZoneOffset.of(<span class="string">&quot;+8&quot;</span>)));</span><br><span class="line">    countMap.put(<span class="string">&quot;view&quot;</span>, postVo.getViewCount() + <span class="number">1</span>);</span><br><span class="line">    countMap.put(<span class="string">&quot;reply&quot;</span>, postVo.getReplyCount());</span><br><span class="line">    countMap.put(<span class="string">&quot;like&quot;</span>, postVo.getLikeCount());</span><br><span class="line">    redisUtil.setHashBatch(countRedisKey, countMap);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="定时任务"><a href="#定时任务" class="headerlink" title="定时任务"></a>定时任务</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@EnableScheduling</span></span><br><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">MyScheduleTaskConfig</span> &#123;</span><br><span class="line">    <span class="meta">@Resource</span></span><br><span class="line">    <span class="keyword">private</span> RedisUtil redisUtil;</span><br><span class="line">    <span class="meta">@Resource</span></span><br><span class="line">    <span class="keyword">private</span> PostService postService;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * key的有效时间</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">KEY_VALID_SECOND</span> <span class="operator">=</span> <span class="number">30</span> * <span class="number">60</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 帖子浏览量、点赞量、回复量</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Scheduled(initialDelay = 10000, fixedDelay = 70000)</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">postCountScheduleTask</span><span class="params">()</span> &#123;</span><br><span class="line">        log.info(LocalDateTime.now() + <span class="string">&quot; -- 开始将Redis中post:count更新到数据库&quot;</span>);</span><br><span class="line">        Set&lt;String&gt; keys = redisUtil.getKeys(RedisKey.POST_COUNT_PREFIX + <span class="string">&quot;*&quot;</span>);</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span> (keys.isEmpty()) &#123;</span><br><span class="line">            log.info(<span class="string">&quot;post:count没有数据，执行结束&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        log.info(keys.toString());</span><br><span class="line"></span><br><span class="line">        <span class="type">Post</span> <span class="variable">tempPost</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Post</span>();</span><br><span class="line">        <span class="type">long</span> <span class="variable">currentSecond</span> <span class="operator">=</span> LocalDateTime.now().toEpochSecond(ZoneOffset.of(<span class="string">&quot;+8&quot;</span>));</span><br><span class="line"></span><br><span class="line">        keys.forEach(redisKey -&gt; &#123;</span><br><span class="line">            Map&lt;Object, Object&gt; hashAll = redisUtil.getHashAll(redisKey);</span><br><span class="line"></span><br><span class="line">            log.info(<span class="string">&quot;处理&quot;</span> + redisKey);</span><br><span class="line">            <span class="comment">// 判断是否更新过</span></span><br><span class="line">            <span class="keyword">if</span> ((<span class="type">int</span>) hashAll.get(<span class="string">&quot;isUpdate&quot;</span>) == <span class="number">1</span>) &#123;</span><br><span class="line">                <span class="comment">// 修改redis中isUpdate</span></span><br><span class="line">                redisUtil.setHash(redisKey, <span class="string">&quot;isUpdate&quot;</span>, <span class="number">0</span>);</span><br><span class="line">                <span class="comment">// 保存到数据库</span></span><br><span class="line">                tempPost.setId(Integer.valueOf(redisKey.substring(RedisKey.POST_COUNT_PREFIX.length())));</span><br><span class="line">                tempPost.setLikeCount((Integer) hashAll.get(<span class="string">&quot;like&quot;</span>));</span><br><span class="line">                tempPost.setViewCount((Integer) hashAll.get(<span class="string">&quot;view&quot;</span>));</span><br><span class="line">                tempPost.setReplyCount((Integer) hashAll.get(<span class="string">&quot;reply&quot;</span>));</span><br><span class="line">                log.info(<span class="string">&quot;更新到数据库 --- &quot;</span> + tempPost);</span><br><span class="line">                postService.updateById(tempPost);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// 判断上次修改时间是否大于30分钟</span></span><br><span class="line">                <span class="keyword">if</span> (currentSecond - (<span class="type">int</span>) hashAll.get(<span class="string">&quot;updateTimeStamp&quot;</span>) &gt;= KEY_VALID_SECOND) &#123;</span><br><span class="line">                    <span class="comment">// 大于30分钟则删除key</span></span><br><span class="line">                    log.info(redisKey + <span class="string">&quot; --- 已过期删除&quot;</span>);</span><br><span class="line">                    redisUtil.delete(redisKey);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">        log.info(LocalDateTime.now() + <span class="string">&quot; -- 将Redis中post:count更新到数据库执行结束&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


        
            <div id="toc-article">
                
  <div class="widget-wrap" id="toc-wrap">
    <h3 class="widget-title"><i class="fa fa-toc"></i> 文章目录</h3>
    <div class="widget">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%89%A7%E8%A1%8C%E9%80%BB%E8%BE%91"><span class="toc-text">执行逻辑</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E4%BB%A3%E7%A0%81"><span class="toc-text">逻辑代码</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%9A%E5%8A%A1%E4%BB%A3%E7%A0%81"><span class="toc-text">业务代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1"><span class="toc-text">定时任务</span></a></li></ol></li></ol>
    </div>
  </div>


            </div>
        
        
      
    </div>
    <footer class="article-footer">
      
        <div class="article-tag-wrap">
          

          
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/wei-blog/tags/%E9%80%BB%E8%BE%91/" rel="tag">逻辑</a></li></ul>

          


        </div>
      
      <!-- 
        
<nav id="article-nav">
  
    <a href="/wei-blog/source/RabbitMQ%E4%BD%BF%E7%94%A8%E5%BB%B6%E8%BF%9F%E9%98%9F%E5%88%97/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">older</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/wei-blog/source/%E6%B3%A8%E8%A7%A3%E7%9A%84%E4%BD%BF%E7%94%A8/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">newer</strong>
      <div class="article-nav-title">
        
          注解的使用
        
      </div>
    </a>
  
</nav>

       -->
      <!-- 
        








       -->
    </footer>
  </div>
</article>
</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-classify"></i> 分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/Docker/">Docker</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/Java/">Java</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/RabbitMQ/">RabbitMQ</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/Redis/">Redis</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/Spring/">Spring</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/SpringBoot/">SpringBoot</a><span class="category-list-count">13</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/SpringBoot/RabbitMQ/">RabbitMQ</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/SpringSecurity/">SpringSecurity</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-tag"></i> 标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/AOP/" rel="tag">AOP</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/JWT/" rel="tag">JWT</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/MyBatis/" rel="tag">MyBatis</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/MyBatisPlus/" rel="tag">MyBatisPlus</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/%E6%8B%A6%E6%88%AA%E5%99%A8/" rel="tag">拦截器</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/%E8%B7%A8%E5%9F%9F/" rel="tag">跨域</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/%E8%BF%90%E8%A1%8C%E9%94%99%E8%AF%AF/" rel="tag">运行错误</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/%E9%80%BB%E8%BE%91/" rel="tag">逻辑</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-posts"></i> 最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/wei-blog/source/%E5%9F%BA%E4%BA%8ERedis%E7%9A%84%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/">基于Redis的分布式锁</a>
          </li>
        
          <li>
            <a href="/wei-blog/source/SpringBoot%E5%AE%9E%E7%8E%B0%E6%8E%A5%E5%8F%A3%E9%99%90%E6%B5%81/">SpringBoot实现接口限流</a>
          </li>
        
          <li>
            <a href="/wei-blog/source/%E7%BC%93%E5%AD%98%E7%A9%BF%E9%80%8F%E3%80%81%E7%BC%93%E5%AD%98%E9%9B%AA%E5%B4%A9%E3%80%81%E7%BC%93%E5%AD%98%E5%87%BB%E7%A9%BF/">(no title)</a>
          </li>
        
          <li>
            <a href="/wei-blog/source/SpringBoot%E6%95%B4%E5%90%88RabbitMQ/">SpringBoot整合RabbitMQ</a>
          </li>
        
          <li>
            <a href="/wei-blog/source/Redis%E4%B9%8BZSet/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>


  
</aside>
        
      </div>
      <a id="totop" href="#top"></a>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      <!-- <p>
        <a href="/wei-blog/sitemap.xml">网站地图</a>
        <span> | </span><a href="/wei-blog/atom.xml">订阅本站</a>
        <span> | </span><a href="/wei-blog/about/">联系博主</a>
      </p> -->
      
      <p>
        <span>Copyright &copy; 2023 xueweitao.</span>
        <!-- <span>Theme by <a href="https://github.com/chaooo/hexo-theme-BlueLake/" target="_blank">BlueLake.</a></span>
        <span>Powered by <a href="https://hexo.io/" target="_blank">Hexo.</a></span> -->
      </p>
    </div>
  </div>
</footer>

    </div>
  </div>
  
<script src="/wei-blog/js/jquery-3.4.1.min.js"></script>


<script src="/wei-blog/js/search.json.js"></script>




<script src="/wei-blog/js/script.js"></script>








  
  



  

  

  

  

  

  

  

  
  





</body>
</html>